package com.example.myviewmodelframe.model

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.myviewmodelframe.base.model.BaseViewModel
import com.example.myviewmodelframe.base.model.BaseViewModelFactory
import com.example.myviewmodelframe.model.bean.UserInfoBea
import com.example.myviewmodelframe.model.bean.UserInfoBea2

/**
 * @author tgw
 * @date 2021/6/11
 * @describe
 */
class MainActivityViewModel(var application: Application,var a:String) : BaseViewModel() {
    private var userInfoModel: MutableLiveData<UserInfoBea>? = null
    private var mainActivityViewModel2: MainActivityViewModel2? = null

    init {
        Log.d("tgw", "view model:带参数$a ")
        userInfoModel = MutableLiveData<UserInfoBea>()
        //第二层的model
        mainActivityViewModel2 = BaseViewModelFactory().create(MainActivityViewModel2::class.java)
    }

    fun createButtonText() {
        var bea = UserInfoBea().apply {
            name = "tgw按钮"
            age = 23
        }
        var h = Handler(Looper.myLooper()!!)
        h.postDelayed(Runnable {
            userInfoModel?.value = bea
            /**多层级model调用时机*/
            mainActivityViewModel2?.createButtonText()

        }, 5000)
    }

    fun getLiveData(): MutableLiveData<UserInfoBea>? {
        //其他的model放在这里才能获取 生命周期
        createMainActivityViewModel2lifecycleOwner()

        return userInfoModel
    }

    private fun createMainActivityViewModel2lifecycleOwner() {
        mainActivityViewModel2?.model?.observe(lifecycleOwner, {
            Log.d("tgw", "MainActivityViewModel2: 封装二${it.toString()}")
        })
    }
}